package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NAsphaltMatchNameBo;
import com.ruoyi.zhgd.domain.vo.NAsphaltMatchNameVo;
import com.ruoyi.zhgd.domain.NAsphaltMatchName;
import com.ruoyi.zhgd.mapper.NAsphaltMatchNameMapper;
import com.ruoyi.zhgd.service.INAsphaltMatchNameService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 级配类型Service业务层处理
 *
 * @author tll
 * @date 2024-01-18
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NAsphaltMatchNameServiceImpl implements INAsphaltMatchNameService {

    private final NAsphaltMatchNameMapper baseMapper;

    /**
     * 查询级配类型
     */
    @Override
    public NAsphaltMatchNameVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询级配类型列表
     */
    @Override
    public TableDataInfo<NAsphaltMatchNameVo> queryPageList(NAsphaltMatchNameBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NAsphaltMatchName> lqw = buildQueryWrapper(bo);
        Page<NAsphaltMatchNameVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询级配类型列表
     */
    @Override
    public List<NAsphaltMatchNameVo> queryList(NAsphaltMatchNameBo bo) {
        LambdaQueryWrapper<NAsphaltMatchName> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NAsphaltMatchName> buildQueryWrapper(NAsphaltMatchNameBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NAsphaltMatchName> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDeptPkid()), NAsphaltMatchName::getDeptPkid, bo.getDeptPkid());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptId()), NAsphaltMatchName::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), NAsphaltMatchName::getType, bo.getType());
        lqw.like(StringUtils.isNotBlank(bo.getName()), NAsphaltMatchName::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getInfo()), NAsphaltMatchName::getInfo, bo.getInfo());
        lqw.eq(bo.getCreator() != null, NAsphaltMatchName::getCreator, bo.getCreator());
        lqw.eq(bo.getCreateDate() != null, NAsphaltMatchName::getCreateDate, bo.getCreateDate());
        lqw.eq(bo.getUpdater() != null, NAsphaltMatchName::getUpdater, bo.getUpdater());
        lqw.eq(bo.getUpdateDate() != null, NAsphaltMatchName::getUpdateDate, bo.getUpdateDate());
        return lqw;
    }

    /**
     * 新增级配类型
     */
    @Override
    public Boolean insertByBo(NAsphaltMatchNameBo bo) {
        NAsphaltMatchName add = BeanUtil.toBean(bo, NAsphaltMatchName.class);
        validEntityBeforeSave(add);
        add.setCreator(LoginHelper.getUserId());
        add.setCreateDate(new Date());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改级配类型
     */
    @Override
    public Boolean updateByBo(NAsphaltMatchNameBo bo) {
        NAsphaltMatchName update = BeanUtil.toBean(bo, NAsphaltMatchName.class);
        validEntityBeforeSave(update);
        update.setUpdater(LoginHelper.getUserId());
        update.setUpdateDate(new Date());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NAsphaltMatchName entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除级配类型
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取配方名称下拉列表数据
     * @param bo
     * @return
     */
    @Override
    public R<List<NAsphaltMatchNameVo>> listSelect(NAsphaltMatchNameBo bo) {
        List<NAsphaltMatchNameVo> data = baseMapper.listSelect(bo);
        return R.ok(data);
    }

    /**
     * 获取配方名称分页
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<NAsphaltMatchNameVo> page(NAsphaltMatchNameBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.page(bo, pageQuery.build()));
    }
}
